Login to your ArcGIS Organization and hide the password with the getpass module. The API represents a GIS portal using the GIS class from the gis module. Instantiate your own GIS portal with this class and use its properties property (the Property Manager object accessed as a property) to access various information. You can also use the User Manager helper class by accessing the users property of the gis class to work with user infromation.
In [ ]:
from arcgis.gis import GIS
import getpass
password = getpass.getpass("Enter your ArcGIS Organizational Account Password: ")
gis = GIS("https://esrihax.maps.arcgis.com", "johnyHack", password)
print("Logged in successfully to {} as {}.".format(gis.properties.urlKey + '.' + gis.properties.customBaseUrl, \
gis.users.me.username))
For more information on IPython.display, refer to IPython's Rich Display System. Importing the diplay function allows for display of Item html and other graphic representations of objects.
Be sure to set your map to a variable for referencing later in the notebook.
In [ ]:
from IPython.display import display
map = gis.map("Springfield, IL", 12)
map
Access the Content Manager with the content property of the gis class. The content object that results contains methods for working with the items in the gis portal.
The search method of the content object allows you to invesigate data you have access to (as determined by the authorization of the currently logged in user.) The search method has various parameters. Find out about the various parameters in the Content Manager help. Below we'll return a list of all the Feature Service items in a portal with the word 'Church' in the title by using a wildcard for the query parameter, Feature Service for the item_type parameter. Set the max_items parameter to control the number of results returned.
In [ ]:
feat_services = gis.content.search(query="title:Church*", item_type="Feature Service", max_items=15)
print("{} has access to {} feature service items.".format(gis.users.me.username, len(feat_services)))
Correct results from API functions require input of specific formats, so it's important to know what types of objects are returned from queries. The Search method on the Content Manager returns a Python list of ArcGIS Items. Accessing members from a list and obtaining properties from items require knowing specific functions and methods. Typing an object, followed by a period, followed by a Tab provides access to properties and methods accessible from objects.
The code below loops the list of Feature Service items to access the unique url to the feature service endpoint. This is a way to check the data and what format is returned from various queries.
In [ ]:
for feat_svc in feat_services:
print("{} is a {}.".format(feat_svc.title.capitalize(), type(feat_svc)))
print("\t{}".format(feat_svc.url))
Use indexing to obtain the item from the list.
In [ ]:
churches_itm = [feat_services][0]
churches_itm
In [ ]:
map.add_layer(churches_itm)
Scroll up to the map to verify the layer is added.